<template>
	<view class="content">
		<nav-bar  home :bgColor="['#CCCCCC','#CCCCCC']" fontColor="#333333" title="新建羊只"></nav-bar>
		<!-- <view class="row b-b">
			<text class="tit">请扫耳标</text>
			<input class="input" type="text" v-model="address.name" placeholder="请输入耳标编码"
				placeholder-class="placeholder" />
		</view> -->
		
		<uni-forms-item required label="请扫耳标">
			<uni-easyinput v-model="addSheepFormData.sheepNo" placeholder="请输入融资单号" />
		</uni-forms-item>
		<!-- <view class="row b-b">
		
			<uni-forms-item  label="选择羊舍">
				<uni-data-select @change="yangsheChange" :localdata="yangsheList" v-model="addSheepFormData.warehouseId" placeholder="请选择羊舍">
				</uni-data-select>
			</uni-forms-item>
			<uni-forms-item  label="羊圈">
				<uni-data-select @change="yangjuanChange" :localdata="yangjuanList" v-model="addSheepFormData.warehouseAreaNo" placeholder="羊圈">
				</uni-data-select>
			</uni-forms-item>
			<uni-forms-item  label="羊栏">
				<uni-data-select  :localdata="yanglanList" v-model="addSheepFormData.warehouseLocationNo" placeholder="羊栏">
				</uni-data-select>
			</uni-forms-item>
			
			
		</view> -->
		<uni-forms-item required label="出生年月">
			<uni-datetime-picker v-model="addSheepFormData.birthDate"
				placeholder="请选择出生年月"></uni-datetime-picker>
		</uni-forms-item>
		<!-- <view class="row b-b">
			<text class="tit">出生年月</text>
			
			<input class="input" type="number" v-model="addSheepFormData.birthDate" placeholder="请输入纳税人识别号"
				placeholder-class="placeholder" />
		</view> -->
		<uni-forms-item required label="性别">
			
			<uni-data-select  :localdata="sexList" v-model="addSheepFormData.sex" placeholder="性别">
			</uni-data-select>
		</uni-forms-item>
		<uni-forms-item required label="羊只种类">
			<!-- <uni-easyinput v-model="addSheepFormData.sex" placeholder="请输入融资单号" /> -->
			<uni-data-select  :localdata="sheepList" v-model="addSheepFormData.sheepType" placeholder="羊只种类">
			</uni-data-select>
		</uni-forms-item>
		<view class="row b-b">
			<text class="tit">当前体重</text>
			<input class="input" type="number" v-model="addSheepFormData.weight" placeholder="请输入羊体重"
				placeholder-class="placeholder" />
		</view>
	<!-- 	<view class="row b-b">
			<text class="tit">开户行及账号</text>
			<input class="input" type="number" v-model="address.account" placeholder="请输入开户行及账号"
				placeholder-class="placeholder" />
		</view> -->
		<!-- <view class="row b-b">
			<text class="tit">省市区</text>
			<pick-regions :defaultRegion="defaultRegion" @getRegion="handleGetRegion">
				<view style="height: 70rpx;border-radius: 4px;width: 100%;">
					<view v-if="!regionName"
						style="color: #cecece;font-size: 30rpx;line-height: 70rpx;margin-left: 20rpx;">
						请选择城市
					</view>
					<view v-else style="color: #000000;font-size: 24rpx;line-height: 70rpx;margin-left: 20rpx;">
						{{regionName}}
					</view>
				</view>
			</pick-regions>
		</view> -->
		<!-- <view class="row b-b">
			<text class="tit">详细</text>
			<input class="input" type="text" v-model="address.address" placeholder="街道、楼号、门牌"
				placeholder-class="placeholder" />
		</view> -->
		<!-- <view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="address.isDefault" color="#fa436a" @change="handleIsDefaultChange" />
		</view> -->
		<button class="add-btn" type="primary" @click="handleSubmit">提交</button>
	</view>
</template>

<script>
	import pickRegions from '@/components/pick-regions/pick-regions.vue'
import uniList from '@/components/uni-list/uni-list.vue';
	export default {
		components: {
			pickRegions
		},
		data() {
			return { 
			sheepList: [{
					text: '种公羊', 
					value: '种公羊'
				},
				{
					text: '种母羊',
					value: '种母羊'
				},					
				{
					label: '羊羔',
					value: '羊羔'
				},
				{
					label: '可出栏',
					value: '可出栏'
				},
				{
					label: '商品羊',
					value: '商品羊'
				}
			],
				sexList: [{
						text: '公',
						value: '公'
					},
					{
						text: '母',
						value: '母'
					}
					
				],
				mapAddress: '', // 地图上的地址
				defaultRegion: ['广西壮族自治区', '柳州市', '柳南区'],
				region: [],
				addSheepFormData: {
					id: '',
					supplierNo: '', //农户编号
					warehouseId: '', //收款企业号
					warehouseAreaNo: '', //收款企业
					warehouseLocationNo: '',
					sheepType: '',
					qualityEval: '',
					sheepBelongTo: '', //融资金额
					sheepNo: '', //融资产品名
					motherNo: '', //融资产品号
					fatherNo: '',
					status: '',
					birthDate: '', //预期用款日
					sex: '', //到期日
					farmType: ''
				},
				address: {
					name:'',
					code:'',
					address:'',
					phone:'',
					account:'',
				},
				regionList: [],
				yangjuanList: [],
				yangsheList: [],
				yanglanList: [],
				selectMultiple: {
					show: false,
					index: []
				},
				form: {
					selectValue: '',
					selectIndex: [],
				},
				selectMultiple1: {
					show: false,
					index: []
				},
				selectedRegion: [],
			}
		},
		watch: {
			selectedRegion(val, oldVal) {
				if (val != null) {
					this.address.province = val[0];
					this.address.city = val[1];
					this.address.district = val[2];
				} else {
					this.address.province = null
					this.address.city = null
					this.address.district = null
				}
			}
		},
		computed: {
			regionName() {
				// 转为字符串
				return this.region.map(item => item.name).join('/')
			}
		},
		onLoad(e) {
			//let title = '新增收货地址';
			/* if (option.id) {
				title = '编辑收货地址'
				this.$api.order.userAddress.get(option.id).then(res => {
					this.address = res
					this.selectedRegion = [res.province, res.city, res.district]
					this.regionName = res.provinceName + ' ' + res.cityName + ' ' + res.districtName
				});
			} */
			//this.yangsheSearch
		
			this.addSheepFormData.sheepBelongTo = e.supplierNo
			this.addSheepFormData.supplierNo = e.supplierNo
			
			/* uni.setNavigationBarTitle({
				title
			}) */
		},
		methods: {
			handleGetRegion(region) {
				console.log(region.map(item => item.name).join('/'))
				// if(this.formData.pickupAddress == ''){
				// 	this.formData.pickupAddress = region.map(item => item.name).join('/')
				// }
				// this.stowageFormData.departureIdList = [];
				let idList = [];
				for (let i in region) {
					idList.push(region[i].code)
				}
				this.address.city = idList.join(',')
				console.log(this.address.city)
				this.region = region
				console.log(region)
			},
			yangsheSearch() {
				let opt = {
					url: 'basic/warehouse/search',
					method: 'POST'
				};
				this.$http.httpTokenRequest(opt, {}).then(res => {
					for(let i in res.data.data){
						this.yangsheList.push({
							text: res.data.data[i].warehouserName,
							value: res.data.data[i].id
						})
					}
				})
			
			},
		
			yanglanChange(e){
				for(let i in this.yangjuanList){
					if(e == this.yangjuanList[i].value){
						//this.addSheepFormData.supplierName = this.yangsheList[i].supplierName
						
					}
				}
			},
			yangjuanChange(e){
				for(let i in this.yangjuanList){
					if(e == this.yangjuanList[i].value){
						//this.addSheepFormData.supplierName = this.yangsheList[i].supplierName
						let opt = {
							url: 'web/tbWmsLocation/search',
							method: 'POST'
						};
						let params = {
							///warehouseId: uni.getStorageSync('userInfo').customerId
							area: e
						};
						this.$http.httpTokenRequest(opt, params).then(res => {
							for(let i in res.data.data){
								this.yanglanList.push({
									text: res.data.data[i].locationName,
									value: res.data.data[i].id
								})
							}
						})
					}
				}
			},
			yangsheChange(e){
				
				for(let i in this.yangsheList){
					
					if(e == this.yangsheList[i].value){
						
						//this.addSheepFormData.supplierName = this.yangsheList[i].supplierName
						let opt = {
							url: 'web/warehouseArea/search',
							method: 'POST'
						};
						let params = {
							///warehouseId: uni.getStorageSync('userInfo').customerId
							warehouseId: e
						};
						this.$http.httpTokenRequest(opt, params).then(res => {
							for(let i in res.data.data){
								this.yangjuanList.push({
									text: res.data.data[i].areaName,
									value: res.data.data[i].id
								})
							}
						})
					}
				}
			},
			/* yangjuanSearch() { //当前客户的羊舍4
				let opt = {
					url: 'basic/hrEmployeeManage/search',
					method: 'POST'
				};
				let params = {
					userType: 'pick'
				};
				this.$http.httpTokenRequest(opt, params).then(res => {
			
					for (let i in res.data.data) {
						this.yangjuanList.push({
							label: res.data.data[i].personName,
							value: res.data.data[i].id
						})
					}
				})
			}, */
			openMap() {
				let that = this;
				uni.chooseLocation({
					success: function(res) {
						that.address.latitude = res.latitude;
						that.address.longitude = res.longitude;
						that.address.address = res.address + res.name;
						that.mapAddress = res.address + res.name;
					}
				});
			},
			handleRegionChange(res) {
				if (res) {
					this.regionName = res.detail.value[0] + ' ' + res.detail.value[1] + ' ' + res.detail.value[2];
					this.selectedRegion = [res.detail.code[0], res.detail.code[1], res.detail.code[2]];
					console.log(this.selectedRegion)
				}
			},
			handleIsDefaultChange(e) {
				console.log(e)
				this.address.isDefault = e.detail.value ? true : false
				console.log(this.address.isDefault)
			},
			// 提交
			handleSubmit() {
				/* if (!this.address.name) {
					vm.$u.toast('请填写收货人姓名');
					return;
				}
				if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(this.address.phone)) {
					vm.$u.toast('请输入正确的手机号码');
					return
				}
				if (!this.address.province) {
					vm.$u.toast('请选择省');
					return
				}
				if (!this.address.city) {
					vm.$u.toast('请选择市')
					return
				}
				if (!this.address.district) {
					vm.$u.toast('请选择区')
					return
				}
				if (!this.address.address) {
					vm.$u.toast('请输入详细地址');
					return
				} */
				if (this.addSheepFormData.id) {
				let opt = {
					url:'basic/gzyySheepInfor',
					method:'PUT'
				};
				this.$http.httpTokenRequest(opt,this.addSheepFormData).then(res => {
					if(res.data.code == 200){
						uni.showToast({
							title: res.data.msg,
							icon: 'success',
							duration: 1500
						});
						setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
						},1500)
					}
					else{
						uni.showToast({
							title: res.data.msg,
							icon: 'error',
							duration: 1500
						});
					}
				})
				} else {
					let opt = {
						url:'basic/gzyySheepInfor',
						method:'POST'
					};
					this.$http.httpTokenRequest(opt,this.addSheepFormData).then(res => {
						if(res.data.code == 200){
							uni.showToast({
								title: res.data.msg,
								icon: 'success',
								duration: 1500
							});
							setTimeout(()=>{
								uni.navigateBack({
									delta: 1
								})
							},1500)
						}
						else{
							uni.showToast({
								title: res.data.msg,
								icon: 'error',
								duration: 1500
							});
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: #EEEEEE;
		padding-top: 16upx;
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;
		border-bottom: 1px solid #eee;
		.tit {
			flex-shrink: 0;
			width: 220upx;
			font-size: 30upx;

		}

		.input {
			flex: 1;
			font-size: 30upx;

		}

		.icon-shouhuodizhi {
			font-size: 36upx;

		}
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		color: #fff;
		border-radius: 10upx;
	}
</style>